package com.admin;

import com.Constants;
import com.admin.CompanyAction;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;







@SuppressWarnings("unused")
public class CompanyAction extends CompanyService {
	
	private static final long serialVersionUID = 1L;
	
	private Company company;
	private int comid;
	private String comids;
	private String comname;
	private List<Company> companys;

	private int page;
	private int limit;

	private User user;
	private String usercode;
	private String oldcomname;
	

	private String Xgzd;
	private String Value;
	private static String table="\"Company\"";

	public String execute() {
		
			User loginuser = (User)session.get(Constants.USER);
			return "success";
	}
	
	/*//显示公司菜单
		public String dotoshowcomtabe() {
			companys = getcome();
			ajaxJsontable(companys);	
				return null;
			}*/
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void jcsfty() {
		Map params = new HashMap();
		params.put("Comid", comid);
		message=getsfyxty(params);
		ajaxJson (message);
		
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void selectcoms() {
		Map params = new HashMap();
		params.put("table", table);
		params.put("where", "\"Forbidden\"=0 and \"Status\"!=9999");
		params.put("nr", "\"Comid\" as \"key\",\"Comname\" as \"value\",\"Forbidden\",\"Status\"");
		params.put("px", "\"Comid\"");
		List<LinkedHashMap<String, String>>	list =selecttable(params);
		ajaxJson(list);
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
		public String dotoshowcomtabe() {
			Map params = new HashMap();
			int min=(page-1)*limit+1;
			int max=limit*page;
			String tj="rownumber between "+min+" and "+max;
			String like="1=1";
			if(comname!=null) {
				if(!comname.equals("")) {
					like+=" and \"Comname\" like '%"+comname+"%'";
				}
				
			}
			if(comid==-1) {
				Company com=(Company)session.get(Constants.USER_COMPANYS);
				Role role=(Role)session.get(Constants.USER_ROLES);
				
				if(role.getRoleid()!=1) {
					like+=" and \"Comid\" = "+com.getComid();
				}
					
			}
			params.put("Status", "0");
			params.put("fybetween", tj);
			params.put("like", like);
			params.put("nr", "*");
			params.put("table", table);
			params.put("px", "\"Comid\" desc");
			List<LinkedHashMap<String, String>>	list = getlist(params);
			if(list.size()>0) {
				
				String strs = gson.toJson(list.get(0));
				JsonParser parser = new JsonParser();
    			JsonElement jsonEl = parser.parse(strs);

    			JsonObject jsonObj = null;
    			jsonObj = jsonEl.getAsJsonObject();//转换成Json对象
    			String number=jsonObj.get("Count").getAsString();//status节点
				ajaxJsontablefy(number,list);	
					
			}else {
				ajaxJsontablefy("0",null);	
					
			}
				return null;
			}
		
		
	
	
	
		//新增公司
		public void savecompany(){
				if (company == null) {
					message = "参数缺失！";
				}
				
				List<Company>	coms = selectcompanybycomname(company.getComname());
				if (!isListEmpty(coms)) {//判断该集合不为空
						message = "新增公司失败！失败原因：公司名称已存在！";
				   }
				else{
					int result = newcompany(company);
					  if (result == 0) {
						message = "新增公司失败！失败原因：数据库保存失败！";
					    }else {
						message = "新增公司成功！";
					  }
					}
				savelog(message);
				ajaxJson (message);
		}
		
		//删除公司判断
		public void companydel(){
			if(comids == null){
				message = "参数缺失!";
			}
			int result=0;
			result=delectcompany(comids);
			if(result!=0){
				message="删除成功!";
			}else{
				message="删除失败!";
			}
			savelog(message);
			ajaxJson(message);
		}
		
		//修改公司信息
		@SuppressWarnings({ "rawtypes", "unchecked" })
		public void xgcompany(){
			
				if(comid==0) {
				message="-1";
				Map map=new HashMap();
				map.put("Value", "参数丢失，修改失败");
				savelog("参数丢失，修改失败");
				ajaxJson(message);
				return;
			}
			if(Xgzd.equals("Comname")) {
				List<Company> coms = selectcompanybycomname(Value);
				if(coms.size()>0) {
					message="1";
					savelog("公司名称重复，修改失败");
					ajaxJson(message);
					return;
				}
			}
			User loginuser = (User)session.get(Constants.USER);
			
			Map params = new HashMap();
			params.put("Keyidvalue", comid);
			params.put("Keyid", "Comid");
			params.put("Xgzd",Xgzd);
			params.put("Value",Value);
			String msg="";
				int result =companymod(params);
				if(result==0){//修改失败   数据库保存失败
					message="2";
					msg="数据库保存异常败";
					
				}else{//修改成功
					params.put("Xgzd","Xguserid");
					params.put("Value",loginuser.getUserid());
					companymod(params);
					msg="操作成功";
				}
			
			savelog(msg);
			ajaxJson(message);
		}
		
		//停启公司状态
		public void xgstatus(){
			int result = xgcompanystatus(company);
			if(result==0){//修改失败
				message="1";
			}else{
				message="0";//修改成功
			}
			ajaxJson(message);
		}
		

		//显示公司名  返回公司List
		@SuppressWarnings({ "rawtypes", "unchecked" })
		public void selectType(){
			//1.通过Usercode在User表中找到Userid  
			Map map1 = new HashMap();
			map1.put("Usercode",usercode);
			String userid = selectUserid(map1);
			//3.通过Userid在Company表中提取出公司名并返回
			Map map = new HashMap();
			map.put("Userid", userid);
			List<Company> list =selectComnameByComid(map); 
			ajaxJson(list);
		}
		
		@SuppressWarnings({ "unchecked", "rawtypes" })
		public void selectRole(){
			String comids = selectComidByComname(comname);
			Map map1 = new HashMap();
			map1.put("Usercode", usercode);
			String userid = selectUserid(map1);
			Map map = new HashMap();
			map.put("Comid", comids);
			map.put("Userid", userid);
			List<Role> list = selectRolenameByComname(map);
			ajaxJson(list);
		}
	
		
		@SuppressWarnings({ "rawtypes", "unchecked" })
		public void scc(){
			//1.通过选中的公司名 在Company表中找到公司id   → 为要修改的公司id
			//2.通过未修改前的公司名与要修改的公司比较  
			//如果相同 则不修改,   如果不相同 将修改前的公司checked变为0  将要修改的公司变为1
			int result1;
			int result2;
			Map map = new HashMap();
			map.put("Usercode", usercode);
			String userid = selectUserid(map);
			Map map1 = new HashMap();
			map1.put("Userid",userid);
			map1.put("oldComname", oldcomname);
			Map map2 = new HashMap();
			map2.put("Userid", userid);
			map2.put("Comname", comname);
			String msg="";
			if(comname.equals(oldcomname)){//修改前与要修改公司相同
				message="3";//公司相同,无需切换 
				msg="未切换";
				
			}else{ 
				result1 = updateoldCompany(map1);//关闭旧公司
				result2 = updateCompanyByComname(map2);//开启新公司
				if(result1!=0&&result2!=0){
					message="1";//公司修改成功
					msg="操作成功";
					
				}else{
					message="2";//公司修改失败
					msg="操作失败";
				}
			}
			savelog(msg);
			ajaxJson(message);
		}
		
		//用户新增页公司列
		@SuppressWarnings({ "rawtypes", "unchecked" })
		public void getcompanylist(){
			Map map = new HashMap();
			map.put("Status",0);
				List list = selectcomnames(map);
				ajaxJson(list);
		}
	
		

		public Company getCompany() {
			return company;
		}

		public void setCompany(Company company) {
			this.company = company;
		}

		public int getComid() {
			return comid;
		}

		public void setComid(int comid) {
			this.comid = comid;
		}

		public String getComids() {
			return comids;
		}

		public void setComids(String comids) {
			this.comids = comids;
		}

		public String getComname() {
			return comname;
		}

		public void setComname(String comname) {
			this.comname = comname;
		}

		public List<Company> getCompanys() {
			return companys;
		}

		public void setCompanys(List<Company> companys) {
			this.companys = companys;
		}


		public int getPage() {
			return page;
		}

		public void setPage(int page) {
			this.page = page;
		}

		public int getLimit() {
			return limit;
		}

		public User getUser() {
			return user;
		}

		public void setUser(User user) {
			this.user = user;
		}

		public void setLimit(int limit) {
			this.limit = limit;
		}


		public String getUsercode() {
			return usercode;
		}

		public void setUsercode(String usercode) {
			this.usercode = usercode;
		}

		public String getOldcomname() {
			return oldcomname;
		}

		public void setOldcomname(String oldcomname) {
			this.oldcomname = oldcomname;
		}


		public String getXgzd() {
			return Xgzd;
		}

		public void setXgzd(String xgzd) {
			Xgzd = xgzd;
		}

		public String getValue() {
			return Value;
		}

		public void setValue(String value) {
			Value = value;
		}


		

	
}